package com.phpxiu.app.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.phpxiu.app.adapter.holder.CommonOnlineViewHolder;
import com.phpxiu.app.api.listener.OnlineItemClickListener;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.MemberInfo;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by WuJinZhou on 2016/5/13.
 */
public class CommonOnlineListAdapter extends RecyclerView.Adapter<CommonOnlineViewHolder> implements View.OnClickListener{
    public final static String TAG="RoomOnlineListAdapter";
    private List<MemberInfo>mOnline=new ArrayList<MemberInfo>();
    private LayoutInflater mInflater;
    private WeakReference<OnlineItemClickListener>weakCallBack;
    public CommonOnlineListAdapter(Context context, List<MemberInfo> online) {
        mOnline=online;
        mInflater=LayoutInflater.from(context);
    }

    @Override
    public CommonOnlineViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        RelativeLayout view=(RelativeLayout)mInflater.inflate(R.layout.common_top_box_online_list_item,viewGroup,false);
        CommonOnlineViewHolder holder=new CommonOnlineViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(CommonOnlineViewHolder viewHolder, int position) {
        MemberInfo item=mOnline.get(position);
        viewHolder.setOnline(item);
        viewHolder.contentView.setOnClickListener(this);
        viewHolder.head.setImageURI(Uri.parse(item.getAvatar()));
    }

    @Override
    public int getItemCount() {
        return mOnline.size();
    }

    /**
     * 添加监听回调
     */
    public void addOnItemClickListener(OnlineItemClickListener listener){
        if(listener==null){
            if(weakCallBack!=null){
                weakCallBack.clear();
                weakCallBack=null;
            }
        }else{
            weakCallBack=new WeakReference<OnlineItemClickListener>(listener);
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getTag()!=null&&v.getTag() instanceof MemberInfo){
            MemberInfo online=(MemberInfo)v.getTag();
            if(weakCallBack!=null&&weakCallBack.get()!=null){
                weakCallBack.get().onShowUserWindow(online.getUid());
            }
        }
    }
}
